package com.chenxingnet.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chenxingnet.dto.Remind;
import com.chenxingnet.dto.Role;
import com.chenxingnet.dto.UserDepartment;
import com.chenxingnet.dto.Usercontrol;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.service.IRemindService;
import com.chenxingnet.service.WaybillsService;
import com.chenxingnet.tools.MD5Tools;



@Controller
@RequestMapping("/remind")
public class RemindController {
	
	
	@Resource
	IRemindService   iRemindService;
	

	@Resource
	WaybillsService   waybillsService;
	
	/**
	 * 根据部门获取提醒
	 * @return
	 */
	@RequestMapping("/getRemindsBydepidSkip")
	public String getRemindsBydepidSkip(HttpServletRequest request ,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		String deptid = request.getParameter("deptid");
		List<Remind>  reminds= 	iRemindService.getRemindsBydepid(Integer.parseInt(deptid));
		List<Waybills>  ways=new  ArrayList<Waybills>();
		for(int i=0;i<reminds.size();i++){
			     Waybills  wa=    waybillsService.selectById(reminds.get(i).getWid());
			     ways.add(wa);
		}
		model.addAttribute("ways", ways);
		model.addAttribute("reminds", reminds);
		model.addAttribute("deptid", deptid);
		return "tixing";
	}

	
	
	/**
	* @Title: mobile_getRemindsBydepidSkip
	*移动端根据部门获取提醒
	 */
	@RequestMapping("/mobile_getRemindsBydepidSkip")
	public void mobile_getRemindsBydepidSkip(HttpServletRequest request,HttpServletResponse response){
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			try {
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			String deptid = request.getParameter("deptid");
			List<Remind>  reminds= 	iRemindService.getRemindsBydepid(Integer.parseInt(deptid));
			List<Waybills>  ways=new  ArrayList<Waybills>();
			for(int i=0;i<reminds.size();i++){
				     Waybills  wa=    waybillsService.selectById(reminds.get(i).getWid());
				     ways.add(wa);
			}
			returnJson.put("ways", ways);
			returnJson.put("reminds", reminds);
			returnJson.put("deptid", deptid);
			response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}

	
	
	
	/**
	 * 根据所有部门提醒
	 * @return
	 */
	@RequestMapping("/getRemindsAllSkip")
	public String getRemindsAllSkip(HttpServletRequest request ,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		List<Remind>  reminds= 	iRemindService.getAllReminds();
		List<Waybills>  ways=new  ArrayList<Waybills>();
		for(int i=0;i<reminds.size();i++){
			     Waybills  wa=    waybillsService.selectById(reminds.get(i).getWid());
			     ways.add(wa);
		}
		model.addAttribute("ways", ways);
		model.addAttribute("reminds", reminds);
		return "tixing";
	}
	
	
	/**
	 * 跳转至所有提醒内容页面
	 * @return
	 */
	@RequestMapping("/getRemindsAll")
	public String getRemindsAll(HttpServletRequest request ,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		List<Remind>  reminds= 	iRemindService.getAllReminds();
		List<Waybills>  ways=new  ArrayList<Waybills>();
		for(int i=0;i<reminds.size();i++){
			     Waybills  wa=    waybillsService.selectById(reminds.get(i).getWid());
			     if(wa != null){
			    	 
			    	 ways.add(wa);
			     }
		}
		model.addAttribute("ways", ways);
		model.addAttribute("reminds", reminds);
		return "tixing";
	}
	/**
	 * 根据运单id显示提醒内容的详细运单信息
	 * @return
	 */
	@RequestMapping("/getRemWayById")
	public String getRemWayById(HttpServletRequest request ,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		Integer wid = Integer.valueOf(request.getParameter("wid"));
		Waybills ways = new Waybills();
		Remind remind = new Remind();
		remind.setWid(wid);
		remind.setRreportstart("0");
		int i = iRemindService.updateByWid(remind);
		if(i != 0){
			ways = waybillsService.selectById(wid);
		}
		model.addAttribute("ways", ways);
		return "reminder-detail";
	}
	
	
	/**
	* @Title: mobile_getAllRemind 
	*移动端获取所有提醒
	 */
	@RequestMapping("/mobile_getAllRemind")
	public void mobile_getAllRemind(HttpServletRequest request,HttpServletResponse response){
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			 List<Remind>  reminds=  iRemindService.getAllReminds();//获取提醒列表
			 JSONObject returnJson = new JSONObject();
					returnJson.put("falg","true");
					returnJson.put("reminds", reminds);
				response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}

	
	
	
	
}
